package com.injurydetection.www.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.injurydetection.www.MyApplication;
import com.injurydetection.www.R;
import com.injurydetection.www.bean.DataBean;
import com.injurydetection.www.greendao.DataBeanDao;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Properties;

/**
 * Created by sunguiyong on 2020/8/13
 */
public class DetailPopu extends PopupWindow {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private View mView;
    private static final String TAG = "DetailPopu";
    private TextView xinlv, xingming, score, scoreLevel, chuli, gaoya, diya, xueyang, huxilv, tiwen;
    private RadioGroup eyeRg, yuyanRg, yundongRg;
    private ImageView btchest, btrightup, btrightlow, bthead, btneck, btleftup, btfubu, btleftlow,
            headSelect, neckSelect, rightupSelect, leftupSelect, chestSelect, fubuSelect, rightLowSelect, leftLowSelect;
    private ConstraintLayout scoreCl;

    @SuppressLint("SetTextI18n")
    public DetailPopu(Context context, int position, List<DataBean> dataBeans) {
        super(context);
        setFocusable(true);//聚焦点击事件不会透传给下面的View
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = mLayoutInflater.inflate(R.layout.popu_detail, null);
        setContentView(mView);
        setWidth(1790);
        setHeight(1140);
//                setWidth(1920);
//        setHeight(1140);
        setOutsideTouchable(true);
        scoreCl = mView.findViewById(R.id.score_cl);
        xingming = mView.findViewById(R.id.textView37);
        xinlv = mView.findViewById(R.id.textView11);
        score = mView.findViewById(R.id.textView35);
        scoreLevel = mView.findViewById(R.id.textView38);
        chuli = mView.findViewById(R.id.textView39);
        gaoya = mView.findViewById(R.id.textView14);
        diya = mView.findViewById(R.id.textView16);
        xueyang = mView.findViewById(R.id.textView19);
        huxilv = mView.findViewById(R.id.textView22);
        tiwen = mView.findViewById(R.id.textView25);
        eyeRg = mView.findViewById(R.id.eye_rg);
        yuyanRg = mView.findViewById(R.id.yuyan_rg);
        yundongRg = mView.findViewById(R.id.yundong_rg);
        btchest = mView.findViewById(R.id.btn_chest);
        btrightup = mView.findViewById(R.id.btn_right_up);
        btrightlow = mView.findViewById(R.id.btn_right_low);
        bthead = mView.findViewById(R.id.btn_head_ok1);
        btneck = mView.findViewById(R.id.btn_neck1);
        btleftup = mView.findViewById(R.id.btn_leftup);
        btfubu = mView.findViewById(R.id.btn_abdomen1);
        btleftlow = mView.findViewById(R.id.btn_left_low);

        headSelect = mView.findViewById(R.id.head_select);
        neckSelect = mView.findViewById(R.id.neck_select);
        rightupSelect = mView.findViewById(R.id.rightup_select);
        leftupSelect = mView.findViewById(R.id.leftup_select);
        chestSelect = mView.findViewById(R.id.chest_select);
        fubuSelect = mView.findViewById(R.id.fubu_select);
        rightLowSelect = mView.findViewById(R.id.rightlow_select);
        leftLowSelect = mView.findViewById(R.id.leftlow_select);


//        List<DataBean> dataBeans = MyApplication.getDaoSession().getDataBeanDao().queryBuilder().orderDesc(DataBeanDao.Properties.CreateTime).list();
        xingming.setText(dataBeans.get(position).getName()
                + "\u3000"
                + dataBeans.get(position).getNumber()
                + "\u3000"
//                + dataBeans.get(position).getBloodType()
        );
        xinlv.setText(dataBeans.get(position).getHeartRate() + "");
        gaoya.setText(dataBeans.get(position).getBloodPressureH() + "");
        diya.setText(dataBeans.get(position).getBloodPressureL() + "");
        xueyang.setText(dataBeans.get(position).getBloodOxygen() + "");
        huxilv.setText(dataBeans.get(position).getRespiratoryRate() + "");
        tiwen.setText(dataBeans.get(position).getTemperature() + "");
        score.setText(dataBeans.get(position).getScore() + "分");

        int shangqingLevel = dataBeans.get(position).getScore();
        if (shangqingLevel >= 12) {
            scoreLevel.setText("轻伤");
            chuli.setText("常规处置");
            score.setTextColor(Color.GREEN);
            scoreLevel.setTextColor(Color.GREEN);
            chuli.setTextColor(Color.GREEN);
            scoreCl.setBackgroundColor(Color.parseColor("#222127"));
        } else if (shangqingLevel >= 10) {
            scoreLevel.setText("中度伤");
            chuli.setText("优先处置");
            score.setTextColor(Color.YELLOW);
            scoreLevel.setTextColor(Color.YELLOW);
            chuli.setTextColor(Color.YELLOW);
            scoreCl.setBackgroundColor(Color.parseColor("#222127"));
        } else if (shangqingLevel >= 6) {
            scoreLevel.setText("重伤");
            chuli.setText("紧急处置");
            score.setTextColor(Color.RED);
            scoreLevel.setTextColor(Color.RED);
            chuli.setTextColor(Color.RED);
            scoreCl.setBackgroundColor(Color.parseColor("#222127"));
        } else {
            scoreLevel.setText("危重伤");
            chuli.setText("期待处置");
            score.setTextColor(Color.BLACK);
            scoreLevel.setTextColor(Color.BLACK);
            chuli.setTextColor(Color.BLACK);
            scoreCl.setBackgroundColor(Color.RED);
        }


        String shanghaiStr = "";
        String chuliStr = "";
        switch (dataBeans.get(position).getScoreLevel()) {
            case 1:
                shanghaiStr = "轻伤";
                chuliStr = "常规处置";
                break;
            case 2:
                shanghaiStr = "中度伤";
                chuliStr = "优先处置";
                break;
            case 3:
                shanghaiStr = "重伤";
                chuliStr = "紧急处置";
                break;
            case 4:
                shanghaiStr = "危重伤";
                chuliStr = "期待处置";

                break;
            default:
                shanghaiStr = "未知";
                break;
        }
        scoreLevel.setText(shanghaiStr);
        chuli.setText(chuliStr);

        switch (dataBeans.get(position).getEye()) {
            case 1:
                eyeRg.check(R.id.eye_rb_1);
                break;
            case 2:
                eyeRg.check(R.id.eye_rb_2);
                break;
            case 3:
                eyeRg.check(R.id.eye_rb_3);
                break;
            case 4:
                eyeRg.check(R.id.eye_rb_4);
                break;
        }

        switch (dataBeans.get(position).getSay()) {
            case 1:
                yuyanRg.check(R.id.yuyan_rb_1);
                break;
            case 2:
                yuyanRg.check(R.id.yuyan_rb_2);
                break;
            case 3:
                yuyanRg.check(R.id.yuyan_rb_3);
                break;
            case 4:
                yuyanRg.check(R.id.yuyan_rb_4);
                break;
            case 5:
                yuyanRg.check(R.id.yuyan_rb_5);
                break;
        }

        switch (dataBeans.get(position).getSport()) {
            case 1:
                yundongRg.check(R.id.yundong_rb_1);
                break;
            case 2:
                yundongRg.check(R.id.yundong_rb_2);
                break;
            case 3:
                yundongRg.check(R.id.yundong_rb_3);
                break;
            case 4:
                yundongRg.check(R.id.yundong_rb_4);
                break;
            case 5:
                yundongRg.check(R.id.yundong_rb_5);
                break;
            case 6:
                yundongRg.check(R.id.yundong_rb_6);
                break;
        }

        if (dataBeans.get(position).getChest()) {
            btchest.setImageResource(R.mipmap.btn_chest_error);
            chestSelect.setVisibility(View.VISIBLE);
        }
        if (dataBeans.get(position).getRightUpperLimb()) {
            btrightup.setImageResource(R.mipmap.btn_right_upper_limb);
            rightupSelect.setVisibility(View.VISIBLE);
        }
        if (dataBeans.get(position).getRightLowLimb()) {
            btrightlow.setImageResource(R.mipmap.btn_right_lower_limb_error);
            rightLowSelect.setVisibility(View.VISIBLE);
        }
        if (dataBeans.get(position).getHead()) {
            bthead.setImageResource(R.mipmap.btn_head_error);
            headSelect.setVisibility(View.VISIBLE);
        }
        if (dataBeans.get(position).getNeck()) {
            btneck.setImageResource(R.mipmap.btn_neck_error);
            neckSelect.setVisibility(View.VISIBLE);
        }
        if (dataBeans.get(position).getLeftUpperLimb()) {
            btleftup.setImageResource(R.mipmap.btn_left_upper_limb_error);
            leftupSelect.setVisibility(View.VISIBLE);
        }
        if (dataBeans.get(position).getAbdomen()) {
            btfubu.setImageResource(R.mipmap.btn_abdomen_error);
            fubuSelect.setVisibility(View.VISIBLE);
        }
        if (dataBeans.get(position).getLeftLowLimb()) {
            btleftlow.setImageResource(R.mipmap.btn_left_lower_limb_error);
            leftLowSelect.setVisibility(View.VISIBLE);
        }
        mView.findViewById(R.id.dismiss_ll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
    }

    public DetailPopu(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}
